package user

import (
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"google.golang.org/grpc/resolver"
	"log"
	"project-api/config"
	"project-common/discovery"
	"project-common/logs"
	loginServiceV1 "project-user/pkg/service/login.service.v1"
	userServiceV1 "project-user/pkg/service/user.service.v1"
)

var loginServiceClient loginServiceV1.LoginServiceClient
var userServiceClient userServiceV1.UserServiceClient

func InitRpcUserClient() {
	log.Println(config.C.EtcdConfig.Addrs)
	etcdRegister := discovery.NewResolver(config.C.EtcdConfig.Addrs, logs.LG)
	resolver.Register(etcdRegister)

	// , grpc.WithDefaultServiceConfig(`"loadBalancingPolicy": "custom_policy"`)
	conn, err := grpc.Dial("etcd:///user", grpc.WithTransportCredentials(insecure.NewCredentials()),
		grpc.WithDefaultServiceConfig(`{"LoadBalancingPolicy": "round_robin"}`))
	if err != nil {
		log.Fatalf("连接grpc服务端失败：%v\n", err)
	}
	// defer conn.Close()
	loginServiceClient = loginServiceV1.NewLoginServiceClient(conn)
	userServiceClient = userServiceV1.NewUserServiceClient(conn)
}
